//
//  Shufflingfigure.m
//  随拍
//
//  Created by lanou3g on 16/1/18.
//  Copyright © 2016年 Ashoka. All rights reserved.
//

#import "Shufflingfigure.h"

@interface Shufflingfigure()<UIScrollViewDelegate>

@property(nonatomic,retain)UIScrollView *scrollView;

@property(nonatomic,retain)UIPageControl *pageControl;

@property(nonatomic,retain)NSTimer *timer;


@end


@implementation Shufflingfigure


-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        _timer = [[NSTimer alloc] init];
        
        //设置默认轮播切换等待时间
        _duration  = 2;
        
    }
    
    return self;
    
}

#pragma mark tap手势的触发方式

-(void)tapAction:(UITapGestureRecognizer *)tap{
    NSUInteger index = tap.view.tag - 1000;
    //将当前的轮播图和当前下标传给外界
    if (_delegate != nil && [_delegate respondsToSelector:@selector(shufflingfigureDidshuffing: atIndex:)]) {
        [_delegate shufflingfigureDidshuffing:self atIndex:index];
    }
    
    
}


#pragma mark代理方法


-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    //开始拖动是停止计时
    [self.timer invalidate];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    self.timer = nil;
    //根据人为拖拽调整pageControl的当前页
    self.pageControl.currentPage = self.scrollView.contentOffset.x/self.frame.size.width;
    //修正当前下标
    self.currentIndex = self.pageControl.currentPage;
    
    //启动timer
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:self.duration target:self selector:@selector(carouseFromTimer:) userInfo:self repeats:YES];
    
    
}

#pragma mark 轮播图的驱动方法

-(void)carouseFromTimer:(id)sender{
    self.currentIndex++;
    if (self.currentIndex == self.imgArray.count) {
        self.currentIndex = 0;
    }
    
    self.pageControl.currentPage = self.currentIndex;
    [self.scrollView setContentOffset:CGPointMake(self.frame.size.width* self.currentIndex,0)];
    
    
}



#pragma mark 赋值方法

-(void)setImgArray:(NSArray *)imgArray{
    //停止timer的操作
    [self.timer invalidate];
    
    self.timer = nil;
    
    _imgArray = imgArray;
    
    [self drawView];
    
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:self.duration target:self selector:@selector(carouseFromTimer:) userInfo:self repeats:YES];
    
    
    
}


-(void)setDuration:(NSTimeInterval)duration{
    
    [self.timer invalidate];
    
    self.timer = nil;
    
    _duration  = duration;
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:self.duration target:self selector:@selector(carouseFromTimer:) userInfo:self repeats:YES];
    
    
}

#pragma mark UI绘制

-(void)drawView{
    //将绘制好的视图添加在父视图上
    
    [self addSubview:self.scrollView];
    [self addSubview:self.pageControl];
    
    
}



-(UIScrollView *)scrollView{
    if (_scrollView == nil) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
        _scrollView.pagingEnabled = YES;
        
        _scrollView.showsHorizontalScrollIndicator  = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.bounces = NO;
        _scrollView.delegate = self;
        
        _scrollView.contentSize = CGSizeMake(self.frame.size.width *self.imgArray.count, self.frame.size.height);
        
        for ( int i = 0 ; i < self.imgArray.count ; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width *i, 0, self.frame.size.width, self.frame.size.height)];
            imageView.userInteractionEnabled = YES;
            
            imageView.image = self.imgArray[i];
            imageView.tag = 1000+i;
            
            //添加手势
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            
            [imageView addGestureRecognizer:tap];
            [_scrollView addSubview:imageView];
            
            
            
            
        }
        
        
    }
    
    return _scrollView;
}

-(UIPageControl *)pageControl{
    if (_pageControl == nil) {
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width-150, self.frame.size.height-40, 150, 40)];
        
        _pageControl.numberOfPages = self.imgArray.count;
        _pageControl.pageIndicatorTintColor = [UIColor grayColor];
        
        _pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
        
    }
    
    return _pageControl;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
